package br.com.skytef.utils;

import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import javax.servlet.http.HttpSession;

import br.com.skytef.jdbc.ConnectionFactory;


public enum utils {;

private static final char[] HEX_CHARS = "0123456789ABCDEF".toCharArray();


public static void criaDBConnection(HttpSession session) throws Exception{
	new ConnectionFactory();
	//	Connection con =  new ConnectionFactory().getConnection();
	Connection con =  ConnectionFactory.getConnection();
	con.setAutoCommit(false);
	session.setAttribute("DBCONNECTIONS", con);
}	
public static Connection recuperaDBConnection(HttpSession session){
	Connection con =  (Connection)session.getAttribute("DBCONNECTIONS");
	
	return con;
}	
public static void execDBCommit(HttpSession session) throws Exception {
	utils.recuperaDBConnection(session).commit();
	utils.recuperaDBConnection(session).close();
	utils.criaDBConnection(session);
}
public static void execDBRollBack(HttpSession session) throws Exception {
	utils.recuperaDBConnection(session).rollback();
	utils.recuperaDBConnection(session).close();
	utils.criaDBConnection(session);
}
public static void renewDBConnection(HttpSession session) throws Exception {
	utils.recuperaDBConnection(session).close();
	utils.criaDBConnection(session);
}
public static void obtemDadosConfigProperties(HttpSession session) throws Exception {
	Properties prop = new Properties();
	prop.load(session.getServletContext().getResourceAsStream("/WEB-INF/config.properties"));
    String cat1 		= prop.getProperty("cat1");
    String cat1item1 	= prop.getProperty("cat1item1");
    String cat1item2 	= prop.getProperty("cat1item2");
    String cat1item3 	= prop.getProperty("cat1item3");
    String cat1item4 	= prop.getProperty("cat1item4");
    String cat1item5 	= prop.getProperty("cat1item5");
    String cat2 		= prop.getProperty("cat2");
    String cat2item1 	= prop.getProperty("cat2item1");
    String cat2item2 	= prop.getProperty("cat2item2");
    String cat2item3 	= prop.getProperty("cat2item3");
    String cat2item4 	= prop.getProperty("cat2item4");
    String cat2item5 	= prop.getProperty("cat2item5");
    String cat3 		= prop.getProperty("cat3");
    String cat3item1 	= prop.getProperty("cat3item1");
    String cat3item2 	= prop.getProperty("cat3item2");
    String cat3item3 	= prop.getProperty("cat3item3");
    String cat3item4 	= prop.getProperty("cat3item4");
    String cat3item5 	= prop.getProperty("cat3item5");
    session.setAttribute("cat1", cat1);
    session.setAttribute("cat1item1", cat1item1);
    session.setAttribute("cat1item2", cat1item2);
    session.setAttribute("cat1item3", cat1item3);
    session.setAttribute("cat1item4", cat1item4);
    session.setAttribute("cat1item5", cat1item5);
    session.setAttribute("cat2", cat2);
    session.setAttribute("cat2item1", cat2item1);
    session.setAttribute("cat2item2", cat2item2);
    session.setAttribute("cat2item3", cat2item3);
    session.setAttribute("cat2item4", cat2item4);
    session.setAttribute("cat2item5", cat2item5);
    session.setAttribute("cat3", cat3);
    session.setAttribute("cat3item1", cat3item1);
    session.setAttribute("cat3item2", cat3item2);
    session.setAttribute("cat3item3", cat3item3);
    session.setAttribute("cat3item4", cat3item4);
    session.setAttribute("cat3item5", cat3item5);
}
public static boolean isValidDate(String date)
{
	SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
	Date testDate = null;
	try
	{
		testDate = sdf.parse(date);
	} catch (java.text.ParseException e) {
		return false;
	}

	if (!sdf.format(testDate).equals(date)) 
	{
		return false;
	}
	return true;
}

public static String asUrlEncode(String s)
{
	int len = s.length();
	String txt="";
	for (int i=0;i<len;i++){
		if (s.charAt(i)=='<')txt+="%3C";
		else if (s.charAt(i)=='=')txt+="%3D";
		else if (s.charAt(i)=='>')txt+="%3E";
		else txt+=s.substring(i,i+1);
	}
	return txt;
}



public static String asHex(byte[] buf)
{
	char[] chars = new char[2 * buf.length];
	for (int i = 0; i < buf.length; ++i)
	{
		chars[2 * i] = HEX_CHARS[(buf[i] & 0xF0) >>> 4];
		chars[2 * i + 1] = HEX_CHARS[buf[i] & 0x0F];
	}
	return new String(chars);
}
public static String hexToASCII(String hex){        
	if(hex.length()%2 != 0){
		System.err.println("requires EVEN number of chars");
		return null;
	}
	StringBuilder sb = new StringBuilder();                

	for( int i=0; i<=hex.length()-1; i+=2 ){

		String output = hex.substring(i, (i + 2));
		
		String bb = hex.substring(i,i+1);
		Integer bbn=0;
		if (bb.charAt(0)<='9'&&bb.charAt(0)>='0') 
			bbn=Integer.parseInt(bb);
		else
			switch (bb.charAt(0))
			{	
			case 'a':
			case 'A':
				bbn=10;break;
			case 'b':
			case 'B':
				bbn=11;break;
			case 'c':
			case 'C':
				bbn=12;break;
			case 'd':
			case 'D':
				bbn=13;break;
			case 'e':
			case 'E':
				bbn=14;break;
			case 'f':
			case 'F':
				bbn=15;break;
			}
		System.out.println("primeiro nible"+bb.charAt(0)+"***"+bbn);

		String cc = hex.substring(i+1,i+2);
		Integer ccn=0;
		if (cc.charAt(0)<='9'&&cc.charAt(0)>='0') 
			ccn=Integer.parseInt(cc);
		else
			switch (cc.charAt(0))
			{	
			case 'a':
			case 'A':
				ccn=10;break;
			case 'b':
			case 'B':
				ccn=11;break;
			case 'c':
			case 'C':
				ccn=12;break;
			case 'd':
			case 'D':
				ccn=13;break;
			case 'e':
			case 'E':
				ccn=14;break;
			case 'f':
			case 'F':
				ccn=15;break;
			}
		System.out.println("segundo nible"+cc.charAt(0)+"***"+ccn);
		
		Character ascii = (char)((bbn<<4)|ccn);
//		Byte ascii = (byte)((bbn<<4)|ccn);
		
		System.out.println("byte em ascii"+ascii);
		
		
		System.out.println("byte a ser convertido:"+output);
		
		/*
		 * Convert Hex to Decimal
		 */
		
//		int decimal = Integer.parseInt(output, 16);                  
//		sb.append((char)decimal);              
		sb.append(ascii);              

		System.out.println("byte convertido.....:"+ascii);
		
	}            
	return sb.toString();
} 

public static String hexToString(String txtInHex)
{
	//	byte [] txtInByte = new byte [txtInHex.length() / 2];
	char [] txtInByte = new char [txtInHex.length() / 2];
	int j = 0;
	short c1=0,c2=0;

	for (int i = 0; i < txtInHex.length(); i += 2)
	{
		switch (txtInHex.substring(i,i+1)) {
		case "0" :	c1=0; 
		case "1" :	c1=1; 
		case "2" :	c1=2; 
		case "3" :	c1=3; 
		case "4" :	c1=4; 
		case "5" :	c1=5; 
		case "6" :	c1=6; 
		case "7" :	c1=7; 
		case "8" :	c1=8; 
		case "9" :	c1=9; 
		case "A" :	c1=10; 
		case "B" :	c1=11; 
		case "C" :	c1=12; 
		case "D" :	c1=13; 
		case "E" :	c1=14; 
		case "F" :	c1=15;
		case "a" :	c1=10; 
		case "b" :	c1=11; 
		case "c" :	c1=12; 
		case "d" :	c1=13; 
		case "e" :	c1=14; 
		case "f" :	c1=15;
		}
		switch (txtInHex.substring(i+1,i+2)) {
		case "0" :	c2=0; 
		case "1" :	c2=1; 
		case "2" :	c2=2; 
		case "3" :	c2=3; 
		case "4" :	c2=4; 
		case "5" :	c2=5; 
		case "6" :	c2=6; 
		case "7" :	c2=7; 
		case "8" :	c2=8; 
		case "9" :	c2=9; 
		case "B" :	c2=11; 
		case "C" :	c2=12; 
		case "D" :	c2=13; 
		case "E" :	c2=14; 
		case "F" :	c2=15;
		case "a" :	c2=10; 
		case "b" :	c2=11; 
		case "c" :	c2=12; 
		case "d" :	c2=13; 
		case "e" :	c2=14; 
		case "f" :	c2=15;
		}
		txtInByte[j++] = (char)((c1<<4) | c2);
		//		txtInByte[j++] = Byte.parseByte(txtInHex.substring(i, i + 2), 16);
	}
	//	return new String(txtInByte);
	return txtInByte.toString();
}

public static char[] stringToChar(String txtInString)
{
	char[] txtInChar = new char [txtInString.length()];
	for (int i = 0; i < txtInString.length(); i += 1)
	{
		txtInChar[i++] = txtInString.charAt(i);
	}
	return txtInChar;
}

}

